﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataProcessModule.Protocol
{
    public class MotorBatteryPackTempratureStats : BaseInstruction
    {
        #region param

        private int SingleBatteriesTotalNumber;//单体蓄电池总数	1	WORD

        private int MotorBatteriesPackNumber;//动力蓄电池包总数N	1	BYTE

        private List<BatteryTempratureInfo> BatteryTempratureInfos;//温度值列表

        #endregion

        #region attributes

        public int AtSingleBatteriesTotalNumber { get { return SingleBatteriesTotalNumber; } }

        public int AtMotorBatteriesPackNumber { get { return MotorBatteriesPackNumber; } }

        public List<BatteryTempratureInfo> AtBatteryTempratureInfos { get { return BatteryTempratureInfos; } }

        #endregion

        #region construction
        public MotorBatteryPackTempratureStats()
        {
            instHeadInfo.instID = 0x0114;
        }

        public MotorBatteryPackTempratureStats(byte[] buf)
        {
            BatteryTempratureInfos = new List<BatteryTempratureInfo>();
            int offset = 0;
            // 获取头信息
            GetHeadInfo(buf, ref offset);
            BodyStreamSplit(buf, ref offset);
        }
        #endregion

        #region function

        public void BodyStreamSplit(byte[] buf, ref int offset)
        {
            try
            {
                SingleBatteriesTotalNumber = buf[offset++];
                MotorBatteriesPackNumber = buf[offset++];
                for (int i = 0; i < MotorBatteriesPackNumber; i++)
                {
                    BatteryTempratureInfo bti = new BatteryTempratureInfo();
                    bti.BatterySequnce = buf[offset++];
                    bti.BatteryProbesNumber = buf[offset++];
                    for (int j = 0; j < bti.BatteryProbesNumber; j++)
                    {
                        bti.ProbeTemprature += buf[offset++].ToString() + ',';
                    }
                    BatteryTempratureInfos.Add(bti);
                }
            }
            catch (Exception e) { }
        }

        public string CombinedSqlString()
        {
            string _sql = string.Empty;
            _sql += "INSERT INTO table_power_temperature_data (TerminalKey," +
                "SingleBatteriesTotalNumber,MotorBatteriesPackNumber,BatteryTempratureInfos) VALUES('" +
                instHeadInfo.tdPhoneNumber + "'," + SingleBatteriesTotalNumber + ',' +
                MotorBatteriesPackNumber + ",'" + ListToString() + "') ON DUPLICATE " +
                "KEY UPDATE SingleBatteriesTotalNumber=VALUES(SingleBatteriesTotalNumber)" +
                ",MotorBatteriesPackNumber=VALUES(MotorBatteriesPackNumber),BatteryTempratureInfos=VALUES(BatteryTempratureInfos);";
            return _sql;
        }
        public string InsertString()
        {
            string _sql = string.Empty;
            _sql += "INSERT INTO table_power_temperature_data_" + DateTime.Now.ToString("yyyyMMdd") + " (TerminalKey," +
                "SingleBatteriesTotalNumber,MotorBatteriesPackNumber,BatteryTempratureInfos) VALUES('" +
                instHeadInfo.tdPhoneNumber + "'," + SingleBatteriesTotalNumber + ',' +
                MotorBatteriesPackNumber + ",'" + ListToString() + "');";
            return _sql;
        }
        public string ListToString()
        {
            string _str = string.Empty;
            foreach (BatteryTempratureInfo bti in BatteryTempratureInfos)
            {
                _str += bti.BatterySequnce.ToString() + '_' + bti.BatteryProbesNumber.ToString() + ':' + bti.ProbeTemprature;
            }
            return _str;
        }

        #endregion
    }

    #region structs

    [Serializable]
    public struct BatteryTempratureInfo
    {
        public int BatterySequnce;

        public int BatteryProbesNumber;

        public string ProbeTemprature;
    }

    #endregion
}
